package com.juyu.miliao.api.service;

import com.juyu.miliao.api.config.IdCreater;
import com.juyu.miliao.api.dao.mysql.UserCoverUrlDao;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.UserCoverUrl;
import com.juyu.miliao.api.util.SystemMessageUtil;
import com.juyu.miliao.common.constant.SpendSystemMessage;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.apache.poi.ss.formula.functions.Now;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

import static com.juyu.miliao.common.msg.CommonErrorMsgs.COMMON_SAVE_ERROR;

@Service
public class UserCoverUrlService {

    @Resource
    private UserCoverUrlDao userCoverUrlDao;
    @Resource
    private IdCreater idCreater;
    @Resource
    private UserRedisDao userRedisDao;
    @Resource
    private UserInfoDao userInfoDao;
    @Autowired
    private SystemMessageUtil systemMessageUtil;


    public List<UserCoverUrl> getUserCoverUrlByUserId(long userId) {
        return userCoverUrlDao.getUserCoverUrlByUserId(userId,0);
    }

    public void deleteUserCoverUrl(int id, long userId) {
        userCoverUrlDao.deleteUserCoverUrl(id, userId);
    }


    @Transactional(rollbackFor = Exception.class)
    public List<UserCoverUrl> batchAddUserCoverUrl(UserCoverUrl entity) {
        if(entity.getCoverUrl()!=null && !entity.getCoverUrl().equals("")){
            List<UserCoverUrl> tempList= new ArrayList<UserCoverUrl>();
            if(entity.getCoverUrl().contains(",")){
                String [] array = entity.getCoverUrl().split(",");
                //需求暂时不需要 前端判断num不能大于5，后台审核判断即可
//                int coverNum = userCoverUrlDao.findCoverNumByUserId(entity.getUserId());
                for(int i = 0,len =array.length;i<len;i++){
                    UserCoverUrl temp = new UserCoverUrl();
                    temp.setUserId(entity.getUserId());
                    temp.setCoverUrl(array[i]);
                    temp.setState(2);
                    temp.setId(idCreater.newCoverUrlIdByUserId(entity.getUserId()));
                    int result = userCoverUrlDao.insertUserCoverUrl(temp);
                    temp.setCreateTime(LocalDateTime.now());
                    tempList.add(temp);
                    //批量添加成功之后 redis 中 添加用户id
                    if(result == 1){
                        User user =userInfoDao.getUserById(temp.getUserId());
                        //审核封面新增用户昵称 便于后台审核处理
                        if(user.getIsHost()==1){
                            userRedisDao.userCoverUrlToHost(temp.getId());
                        }else {
                            userRedisDao.userCoverUrl(temp.getId());
                        }
                    }

                }
                String message = SpendSystemMessage.UPDATE_USER_COVER_URL_SEND_MESSAGE.getMessage();
                systemMessageUtil.sendSystemMessage(entity.getUserId(), message);
            }else{
                entity.setState(2);
                entity.setId(idCreater.newCoverUrlIdByUserId(entity.getUserId()));
                int result = userCoverUrlDao.insertUserCoverUrl(entity);
                if (result == 1) {
                    //添加成功后 存放到redis中
                    User user =userInfoDao.getUserById(entity.getUserId());
                    if(user.getIsHost()==1){
                        userRedisDao.userCoverUrlToHost(entity.getId());
                    }else {
                        userRedisDao.userCoverUrl(entity.getId());
                    }
                    entity.setCreateTime(LocalDateTime.now());
                    tempList.add(entity);
                    String message = SpendSystemMessage.UPDATE_USER_COVER_URL_SEND_MESSAGE.getMessage();
                    systemMessageUtil.sendSystemMessage(entity.getUserId(), message);
                    return tempList;
                }else{
                    throw new ConsumeRuntimeException(COMMON_SAVE_ERROR);
                }
            }

            return tempList;
        }else{
            throw new ConsumeRuntimeException(COMMON_SAVE_ERROR);
        }
    }
}
